var driverId;
var driverName;
$(function () {
	driverId = $.getUrlParam("id");
    driverName = $("#driverName").val();
    
	//检测用户名是否被使用
    $.addCustomAjaxValidity(
        {
            "url": '../driver/getDriverNameCheck.htm',
            "alertssssTextOk":"* ok",
            "alertText": "* 此用户名已被使用",
            "alertTextLoad": "* 正在确认用户名，请稍等..."
        }
    );
   
    var promptMessge = null;
    //验证某一个控件验证完成后，返回的参数
    $('#driverName').bind('jqv.field.result', function(event, field, errorFound, prompText){
    	if(prompText === "* 此用户已经在平台注册，可以直接绑定"){
    		promptMessge = "binding";
    		$("#driverPwd").parent("div").parent("div").empty();	//清空节点
    	}
    });
	
    //是否验证成功
    $('#editDriverForm').validationEngine('attach', {
        validationEventTrigger : 'blur',	//当提交的时候验证是否合法
        promptPosition: 'inline',
        scroll: false,
        focusFirstField : true,
        onValidationComplete : function (form, valid) {
        	if (valid){
    			if(promptMessge == "binding"){
            		addDriverInfo("../driver/addToDriverComnumInfo.htm", {driverName: $("#driverName").val()});
            		return;
            	}
        		addDriverInfo("../driver/addToDriverAndDriverComnumInfo.htm", {driverName: $("#driverName").val(), driverPwd: $("#driverPwd").val()});
            }
        }
    });
});

//关闭窗口
function closeForm() {
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}

//增加
function addDriverInfo(URL, dataParam) {
    $.ajax({
        url: URL,
        cache: false,
        type: "post",
        dataType: "json",
        data: dataParam,
        beforeSend:function(){var index = layer.load(1, { shade: [0.8,'#ddd']});},
        complete:function(){var index = layer.closeAll("loading");},
        success: function (result) {
            if (result.status == 0) {
                parent.layer.msg(result.message, {icon: 1});
                parent.$table.bootstrapTable('refresh', {url: '../driver/getDriverInfoByPage.htm'});
                closeForm();
            } else {
                parent.layer.msg(result.message, {icon: 2});
                closeForm();
            }
        }
    });
}
